#include "threadpro.h"
#include "ui_threadpro.h"
#include <QThread>
#include <QDebug>
#include <QTime>

ThreadPro::ThreadPro(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::ThreadPro)
{
	ui->setupUi(this);
	launch = new Launch;
	thread = new QThread(this);
	launch->moveToThread(thread);

	void (Launch::*sign)(QString) = &Launch::signal;
	connect(launch,sign,this,[=](QString str){
		static int i = 0;
		++i;
		ui->lcdNumber->display(i);
		ui->textBrowser->append(str);
	});
	connect(this,&QWidget::windowTitleChanged,launch,&Launch::timeout);
	//点击大叉×先关闭进程
	connect(this,&QWidget::destroyed,this,&ThreadPro::on_pushButton_stop_clicked);
}

ThreadPro::~ThreadPro()
{
	delete ui;
}

void ThreadPro::on_pushButton_start_clicked()
{
	Launch::son = 0;
	thread->start();
	this->setWindowTitle(QTime::currentTime().toString());
	ui->textBrowser->setText(QString().sprintf("当前主线程ID：%p",QThread::currentThread()));
	qDebug() << QThread::currentThread();
}

void ThreadPro::on_pushButton_stop_clicked()
{
	Launch::son = 10;
	thread->quit();
	thread->wait();
}
